package com.hk.Mgr.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.PointsReq;
import com.hk.Mgr.web.message.kyc.PointsRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.Constant;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.entity.Points;
import com.hk.commons.entity.sys.SysUser;
import com.hk.service.kyc.IPoints;
import com.hk.service.sys.ISysUser;

/**
 * @author lemon 用户实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/pointsCtrl")
public class PointsRuleCtrl extends BaseCtrl {

	@Autowired
	private ISysUser userService;

	@Autowired
	private IPoints pointsService;

	/**
	 * 增加积分规则
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResponseBase<Points> login(@RequestBody PointsReq req) {
		ResponseBase<Points> resp = new ResponseBase<Points>();

		String typeName = req.getTypeName();
		Integer scoreType = req.getScoreType();
		Integer score = req.getScore();
		Long creatorId = req.getCreator();

		SysUser creator = userService.findById(creatorId);
		try {

			logger.info("mgr/PointsCtl/add type = " + typeName
					+ " scoreType = " + scoreType + "   score = " + score + "  creator Id = " + creatorId);

			if (StringUtils.isBlank(typeName) || scoreType == null
					|| score == null || creator == null) {
				resp.setMsg("参数错误，部分参数为空");
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				logger.error("mgr/PointsCtl/add some input field is empty");
				return resp;
			}
			
			Points points = new Points();
			ReqToModelHelper.copyReqValueToModel(req, points);
			//设置默认为不启动
			points.setStatus(Constant.POINTS_ROULE_NO_ACTIVE);
			pointsService.add(points);
			resp.setMsg("添加成功");
			resp.setResult(points);
			resp.setResultCode(StatusCode.SUCCESS.key());
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * 删除积分规则
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ResponseBase<PointsRes> delete(@RequestBody PointsReq req) {
		ResponseBase<PointsRes> resp = new ResponseBase<PointsRes>();

		Integer id = req.getId();
		Long creatorId = req.getCreator();

		SysUser creator = userService.findById(creatorId);

		logger.info("mgr/PointsCtl/delete id = " + id + " creatorId = "
				+ creatorId);

		if (id == null || creator == null) {
			resp.setMsg("参数错误，部分参数为空");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			logger.error("mgr/PointsCtl/delete some input field is empty");
			return resp;
		}

		Points points = pointsService.findById(id);
		if (points == null) {
			resp.setMsg("改记录不存在");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			logger.error("mgr/PointsCtl/delete the record is not exist! ");
			return resp;
		}

		if (points != null) {
			pointsService.delete(points);
			resp.setMsg("删除成功");
			resp.setResultCode(StatusCode.SUCCESS.key());
		}
		return resp;
	}

	/**
	 * 更新积分规则
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseBase<Boolean> update(@RequestBody PointsReq req) {
		ResponseBase<Boolean> resp = new ResponseBase<Boolean>();

		String typeName = req.getTypeName();
		Integer scoreType = req.getScoreType();
		Integer score = req.getScore();
		Integer status = req.getStatus();
		Long creatorId = req.getCreator();

		SysUser creator = userService.findById(creatorId);
		try {

			logger.info("mgr/PointsCtl/update type = " + typeName
					+ " scoreType = " + scoreType + "   score = " + score
					+ " status = " + status + "  creator Id = " + creatorId);

			if (StringUtils.isBlank(typeName) || scoreType == null
					|| score == null || status == null || creator == null) {
				resp.setMsg("参数错误，部分参数为空");
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				logger.error("mgr/PointsCtl/update some input field is empty");
			}

			Points points = new Points();
			points.setCts(System.currentTimeMillis());
			ReqToModelHelper.copyReqValueToModel(req, points);
			pointsService.update(points);
			resp.setResult(true);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("更改成功");
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResult(false);
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
	
	/**
	 * 更新积分规则的状态
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
	public ResponseBase<Boolean> updateStatus(@RequestBody PointsReq req) {
		ResponseBase<Boolean> resp = new ResponseBase<Boolean>();

		 
		Points points = pointsService.findById(req.getId());
		SysUser user = userService.findById(req.getCreator());
		Integer status = req.getStatus();
		try {
			
			if(points == null ||  user == null || status == null){
				resp.setMsg("参数错误，部分参数为空");
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				logger.error("mgr/PointsCtl/updateStatus pointsId = " + req.getId()
				+ "  status = " + req.getStatus() + "  creator = " + req.getCreator() );
				logger.error("mgr/PointsCtl/updateStatus some input field is empty");
			}
			points.setStatus(status);
			pointsService.update(points);
			resp.setResult(true);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("更改成功");
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
	
	
	/**
	 * 获取积分规则列表
	 * @param  typeName, curPage, pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public ResponseBase<List<PointsRes>> list(@RequestBody PointsReq req) {
		ResponseBase<List<PointsRes>> resp = new ResponseBase<List<PointsRes>>();
		List<PointsRes> pointsResList = new ArrayList<PointsRes>();
		String typeName = req.getTypeName();
		
		logger.info("mgr/PointsCtl/update type = " + typeName );
		 
		HqlFilter hqlFilter = new HqlFilter();
		if (StringUtils.isNotEmpty(req.getTypeName())) {
			hqlFilter.addLKFilter("typeName", typeName);
		}
		hqlFilter.addSort("id");
		hqlFilter.addOrder("desc");
		
		try {
			List<Points> retList = pointsService.findByFilter(hqlFilter,
					req.getCurPage(), req.getPageSize());
			for(Points points:retList){
				PointsRes pointsRes = new PointsRes();
				ReqToModelHelper.copyReqValueToModel(points, pointsRes);
				//添加船建造姓名
				pointsRes.setCreatorName(userService.getNameById(pointsRes.getCreator()));
				pointsResList.add(pointsRes);
			}
			resp.setResult(pointsResList);
			resp.setSize(pointsResList.size());
			resp.setResultCode(StatusCode.SUCCESS.key());
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}


}
